//
//  luoerHomeViewController.swift
//  WeiBoStudy
//
//  Created by luoerliu on 16/5/9.
//  Copyright © 2016年 刘文平. All rights reserved.
//

import UIKit

class luoerHomeViewController: luoerBaseViewController {
    private let reseldentifier = "cell"
//    要显示的微博数据
    var statuses: [luoerStatus]?
        {
        didSet
        {
             self.tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
//        如果没有登录就不执行下面的代码，用之前设置的
        if !luoerUserAccountViewModel.sharedinstance.isUserLogin
        {return}
        self.setupNavigationBar()
//        注册cell
        tableView.registerClass(luoerStatusCell.self, forCellReuseIdentifier: reseldentifier)
//        设置cell的预估行高
        tableView.estimatedRowHeight = 200
//        根据cell的约束自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
//        取消tableviewcell的分割线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
//        通过单例加载微博数据
        // 加载微博数据
        luoerStatusViewModel.sharedInstance.loadStatus { (statuses, error) -> () in
            if error != nil {
                return
            }
            // 请求成功了
            // 将网络请求返回的微博模型赋值给控制器的属性
            self.statuses = statuses
        }
        
    }
//设置导航栏
    private func setupNavigationBar()
    {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "didClickFriendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didClickPop")
//        获取用户名称
        let name = luoerUserAccountViewModel.sharedinstance.userAccount?.screen_name ?? "未知名称"
        
//        设置标题栏
        let button = luoerHomeTitleButton()
        button.setTitle(name, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        button.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "didClickTitlView:", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
//        设置标题栏的view
        navigationItem.titleView = button
    }
//    MARK:按钮的点击事件
    func didClickTitlView(button:UIButton)
    {
//        修改按钮的状态
        button.selected = !button.selected
//        根据按钮的点击状态旋转图片
        if button.selected {
                        // 箭头转上去
                        UIView.animateWithDuration(0.25, animations: { () -> Void in
                            button.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001))
                        })
                    } else {
                        UIView.animateWithDuration(0.25, animations: { () -> Void in
                            // 箭头转下来, 清空transform
                            button.imageView?.transform = CGAffineTransformIdentity
            //                button.imageView?.transform = CGAffineTransformRotate(button.imageView!.transform, CGFloat(M_PI))
                        })
                    }
    }
    func didClickFriendsearch() {
      print("点击了左边")
    }
    
    func didClickPop() {
       print("点击了右边")
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
// 扩展CZHomeViewController, talbeView显示数据
extension luoerHomeViewController {
    /// 返回cell的数量
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // ?? 前面的可选没有值就使用??后面的值
        // ?? 前面的可选有值只用??前面的可选,进行后续的操作
        return statuses?.count ?? 0
    }
    
    /// cell显示内容
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 根据identifier去缓存池中查找,缓存池中有就返回一个cell,缓存池中没有,使用注册的cell类型来创建cell
        let cell = tableView.dequeueReusableCellWithIdentifier(reseldentifier, forIndexPath: indexPath) as! luoerStatusCell
        
        // 获取到对应的模型
        let status = self.statuses![indexPath.row]
        
        // 设置cell内容
        cell.status = status
        return cell
    }

    
}
